package net.minecraft.src;

import java.util.Random;

public class BlockAlgae extends Block
{

    public BlockAlgae(int i)
    {
        super(i, Material.pumpkin);
        float f = 0.125F;
        setBlockBounds(0.0F, 0.0F - f, 0.0F, 1.0F, 0.0F - f, 1.0F);
        setTickOnLoad(true);
		blockIndexInTexture = ModLoader.addOverride("/terrain.png", "/pph/algae.png");
		eggTexture = ModLoader.addOverride("/terrain.png", "/pph/eggs.png");
		//gloopTexture = ModLoader.addOverride("/terrain.png", "/pph/gloop.png");
    }
	
	public int getBlockTextureFromSideAndMetadata(int i, int j)
    {
        if(j == 2)
        {
            return eggTexture;
        }
        if(j == 3)
        {
            return blockIndexInTexture; //gloopTexture
        } else
		{
			return blockIndexInTexture;
		}
	}
	
	public int getRenderBlockPass()
    {
        return 1;
    }
	
	public int tickRate()
    {
        return 3;
    }
	
	public void onBlockAdded(World world, int i, int j, int k)
    {
        world.scheduleBlockUpdate(i, j, k, blockID, tickRate());
    }

    public void onNeighborBlockChange(World world, int i, int j, int k, int l)
    {
        world.scheduleBlockUpdate(i, j, k, blockID, tickRate());
    }

    public void updateTick(World world, int i, int j, int k, Random random)
    {
		int l = world.getBlockMetadata(i, j, k);
		if(l != 3 && (world.getBlockMaterial(i, j + 1, k) == Material.water))
        {
		if(world.isAirBlock(i, j + 2, k))
		{
			world.setBlockWithNotify(i, j, k, 0);
			world.setBlockAndMetadataWithNotify(i, j + 2, k, mod_pph.algae.blockID, l);
		} else
		if(world.getBlockMaterial(i, j + 2, k).isSolid())
		{
			world.setBlockWithNotify(i, j, k, 0);
		} else
		{
			world.setBlockWithNotify(i, j, k, 0);
			world.setBlockAndMetadataWithNotify(i, j + 1, k, mod_pph.algae.blockID, l);
		}
        }
		if(world.getBlockId(i, j + 1, k) == mod_pph.algae.blockID)
		{
			world.setBlockWithNotify(i, j, k, 0);
		}
		if(world.getBlockMetadata(i, j, k) == 2 && random.nextInt(5) == 0)
		{
			spawnDragonfly(world, i, j, k);
			spawnDragonfly(world, i + 1, j, k);
			spawnDragonfly(world, i, j, k + 1);
			spawnDragonfly(world, i + 1, j, k + 1);
			world.setBlockWithNotify(i, j, k, 0);
		}
		super.updateTick(world, i, j, k, random);
    }
	
	private void spawnDragonfly(World world, int i, int j, int k)
	{
		EntityDragonfly entitydragonfly = new EntityDragonfly(world);
        entitydragonfly.setLocationAndAngles(i, j + 2, k, 0.0F, 0.0F);
        world.entityJoinedWorld(entitydragonfly);
	}
	
	public void onEntityCollidedWithBlock(World world, int i, int j, int k, Entity entity)
	{
		if(world.getBlockMetadata(i, j, k) != 3 && (entity instanceof EntityPlayer || entity instanceof EntityAnimal || entity instanceof EntityMob))
		{
			world.setBlockWithNotify(i, j, k, 0);
		}
	}
	
	public boolean canPlaceBlockAt(World world, int i, int j, int k)
    {
        return world.getBlockId(i, j, k) == 0 && world.getBlockId(i, j - 1, k) == Block.waterStill.blockID;
    }

    public AxisAlignedBB getCollisionBoundingBoxFromPool(World world, int i, int j, int k)
    {
        return null;
    }

    public int idDropped(int i, Random random)
    {
		if(i == 2)
		{
			return mod_pph.dragonflyEggs.shiftedIndex;
		}/*
		if(i == 3)
		{
			
		}*/ else
		{
			return -1;
		}
    }
	
	public int quantityDropped(Random random)
    {
        return 0;
    }
	
	public boolean renderAsNormalBlock()
    {
        return false;
    }
	
	public int getRenderType()
    {
        return mod_pph.renderBlockAlgae;
    }

    public boolean isOpaqueCube()
    {
        return false;
    }
	
	//private int algaeTexture;
	private int eggTexture;
	//private int gloopTexture;
}
